//
//  SpecificationCell.m
//  YunJi_mianfeigou
//
//  Created by Dailu on 2017/3/31.
//  Copyright © 2017年 Dailu. All rights reserved.
//

#import "SpecificationCell.h"

@interface SpecificationCell ()
@property(nonatomic,strong)UIButton *selectedBtn;

@end

@implementation SpecificationCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
     
    }
    return self;
}
-(void)setModel:(SpecificationModel *)model{
    while ([self.contentView.subviews lastObject] != nil) {
        [(UIView*)[self.contentView.subviews lastObject] removeFromSuperview];  //删除并进行重新分配
    }
    UILabel *title=[UILabel new];
    [self.contentView addSubview:title];
    title.text=model.name;
    title.font=[UIFont systemFontOfSize:13];
    [title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(0);
        make.left.with.offset(20);
        make.height.offset(40);
    }];
    float butX = 20;
    int Y = 0;
  
    for(int i = 0; i < model.val.count; i++){
        ValModel *valModel=model.val[i];
        //宽度自适应
        NSDictionary *fontDict = @{NSFontAttributeName:[UIFont systemFontOfSize:12]};
        CGRect frame = [valModel.name boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:fontDict context:nil];
        float butW=frame.size.width;
        if (butW<10) {
            butW=10;
        }

        UIButton *but = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.contentView addSubview:but];
        [but mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(title.mas_bottom).with.offset(Y*40+0);
            make.left.equalTo(self.contentView).with.offset(butX);
            make.height.mas_equalTo(30);
            make.width.mas_equalTo(butW+20);
        }];
        [but addTarget:self action:@selector(specAction:) forControlEvents:UIControlEventTouchUpInside];
        but.backgroundColor=[UIColor colorWithRed:0.96f green:0.96f blue:0.96f alpha:1.00f];
        [but setTitle:valModel.name forState:UIControlStateNormal];
        [but setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        [but setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        but.titleLabel.font = [UIFont systemFontOfSize:12];
        but.layer.cornerRadius = 15;
        butX+= butW+30;
    }
    
    UILabel *line=[UILabel new];
    line.backgroundColor=[UIColor groupTableViewBackgroundColor];
    [self.contentView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.contentView);
        make.left.equalTo(self.contentView).with.offset(20);
        make.right.equalTo(self.contentView).with.offset(-20);
        make.height.mas_equalTo(1);
    }];
}

-(void)specAction:(UIButton *)sender{
//    NSLog(@"%d------%d",sender.selected,self.selectedBtn.selected);
    if (self.selectedBtn==sender) {
        sender.selected=!sender.selected;
    }else{
        sender.selected=!sender.selected;
        if (self.selectedBtn.selected) {
            self.selectedBtn.selected=!self.selectedBtn.selected;
        }
    }
//    NSLog(@"%d------%d",sender.selected,self.selectedBtn.selected);
    if (sender.selected) {
        sender.backgroundColor=[UIColor orangeColor];
    }else{
        sender.backgroundColor=[UIColor colorWithRed:0.96f green:0.96f blue:0.96f alpha:1.00f];
    }
    if (self.selectedBtn.selected) {
        self.selectedBtn.backgroundColor=[UIColor orangeColor];
    }else{
        self.selectedBtn.backgroundColor=[UIColor colorWithRed:0.96f green:0.96f blue:0.96f alpha:1.00f];
    }
    self.selectedBtn=sender;
    
    
    if (self.delegate&&[self.delegate respondsToSelector:@selector(specAction:andSpec_id:)]) {
        [self.delegate specAction:self andSpec_id:sender];
    }
    
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
